package com.fasteat.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasteat.common.entity.GlobalException;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Orderform;
import com.fasteat.mall.entity.OrderformAftersales;
import com.fasteat.mall.entity.Product;
import com.fasteat.mall.entity.vo.OrderformAfterSalesProductVo;
import com.fasteat.mall.entity.vo.OrderformProductVo;
import com.fasteat.mall.mapper.OrderformAftersalesMapper;
import com.fasteat.mall.service.OrderformAftersalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasteat.mall.service.OrderformService;
import com.fasteat.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hy
 * @since 2021-02-20
 */
@Service
public class OrderformAftersalesServiceImpl extends ServiceImpl<OrderformAftersalesMapper, OrderformAftersales> implements OrderformAftersalesService {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderformService orderformService;


    @Override
    public List<OrderformAfterSalesProductVo> pageVoIAmAngry(Integer start, Integer end, String userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        List<OrderformAftersales> orderformAftersalesList = baseMapper.selectList(wrapper);
        //对start和end进行保护
        int size = orderformAftersalesList.size();
        if(size == 0 || start > size) {
            return null;
        }
        end = end > size ? size : end;
        List<OrderformAfterSalesProductVo> retList = new ArrayList<>();
        for(OrderformAftersales orderformAftersales : orderformAftersalesList) {
            Orderform orderform = orderformService.getById(orderformAftersales.getOrderformId());
            Product product = productService.getById(orderform.getProductId());
            OrderformAfterSalesProductVo orderformAfterSalesProductVo = new OrderformAfterSalesProductVo();
            //封装商品信息
            orderformAfterSalesProductVo.setTitle(product.getTitle());
            orderformAfterSalesProductVo.setAvatar(product.getAvatar());
            orderformAfterSalesProductVo.setSalePrice(product.getSalePrice());
            //封装订单信息
            orderformAfterSalesProductVo.setOrderformId(orderform.getId());
            orderformAfterSalesProductVo.setUserId(orderform.getUserId());
            orderformAfterSalesProductVo.setProductId(orderform.getProductId());
            orderformAfterSalesProductVo.setPurchaseAmount(orderform.getPurchaseAmount());
            orderformAfterSalesProductVo.setAddressId(orderform.getAddressId());
            orderformAfterSalesProductVo.setOrderStatus(orderform.getOrderStatus());
            orderformAfterSalesProductVo.setAftersalesStatus(orderform.getAftersalesStatus());
            orderformAfterSalesProductVo.setGmtCreate(orderform.getGmtCreate());
            orderformAfterSalesProductVo.setGmtModified(orderform.getGmtModified());
            //封装售后信息
            orderformAfterSalesProductVo.setOrderformAftersalesId(orderformAftersales.getId());
            //打入retList
            retList.add(orderformAfterSalesProductVo);
        }
        retList.add(null);
        //切割
        List<OrderformAfterSalesProductVo> retListCopy = retList.subList(start, end);
        return retListCopy;
    }
}
